package catalogue

import (
	"clouds.lgb24kcs.cn/errorx"
	"clouds.lgb24kcs.cn/global"
	"clouds.lgb24kcs.cn/models"
	"clouds.lgb24kcs.cn/utils"
	"github.com/gin-gonic/gin"
	"time"
)

type Home_CatalogueAPI struct{}

var HomeAPI Home_CatalogueAPI

func (a *Home_CatalogueAPI) ALL(c *gin.Context, userId int64) {
	bucketInfo := []models.Storage{}
	catalogueInfo := []models.Catalogue{}
	var Count int64
	tx := global.APP.DB.Where("userid=?", userId).Find(&bucketInfo)
	if tx.Error != nil {
		global.APP.Log.Error(tx.Error.Error())
		utils.Fail(c, errorx.VerifyError, tx.Error.Error())

	} else if tx.RowsAffected == 0 {
		global.APP.Log.Error("数据不存在")
		utils.Fail(c, errorx.VerifyError, "数据不存在")
	} else {
		tmp := []map[string]interface{}{}
		for _, item := range bucketInfo {
			tx = global.APP.DB.Where("bucketid=? and userid=?", item.ID, userId).Find(&catalogueInfo).Count(&Count)
			if tx.Error != nil {
				global.APP.Log.Error(tx.Error.Error())
				utils.Fail(c, errorx.VerifyError, tx.Error.Error())

			} else if tx.RowsAffected == 0 {
				global.APP.Log.Error("数据不存在")
				utils.Fail(c, errorx.VerifyError, "数据不存在")
			} else {
				for _, catalogue := range catalogueInfo {
					tmp = append(tmp, map[string]interface{}{
						"catalogue_id":   catalogue.ID,
						"catalogue_name": catalogue.Diskname,
						"bucket_id":      catalogue.Bucketid,
						"bucket_name":    item.Bucketname,
						"user_id":        item.Userid,
						"create_at":      catalogue.CreatedAt.Format(time.DateTime),
					})
				}
			}
		}
		//if err := history.HistoryCreate(userId, c.Request.Method, c.Request.URL.String(), c.Request.Host); err != nil {
		//	utils.Fail(c, errorx.ObjectSearch, err.Error())
		//} else {
		//
		//
		//}
		utils.Success(c, Count, tmp)
	}

}
